package com.yuju.hotel.service.impl;

import java.sql.Time;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuju.common.core.dao.IBaseDao;
import com.yuju.common.core.service.AbstractBaseService;
import com.yuju.hotel.dao.ICheckinStandardDao;
import com.yuju.hotel.dao.entity.CheckinStandard;
import com.yuju.hotel.model.CheckinStandardModel;
import com.yuju.hotel.model.PriceDetailModel;
import com.yuju.hotel.service.ICheckinStandardService;
import com.yuju.hotel.service.IPriceDetailService;
import com.yuju.util.Utils;

@Service
public class CheckinStandardServiceImpl extends AbstractBaseService<CheckinStandardModel, CheckinStandard, String> implements ICheckinStandardService{

	@Autowired
	private ICheckinStandardDao checkinStandardDao;
	@Autowired
	private IPriceDetailService priceDetailService;
	
	@Override
	public IBaseDao<CheckinStandardModel, CheckinStandard, String> getDao() {
		// TODO Auto-generated method stub
		return checkinStandardDao;
	}

	/**
	 * 
	 * 功能: 新增入住标准<br/>
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2017年11月29日 下午3:07:56<br/>
	 * 
	 * @param checkinStandardModel
	 * @return <br/>
	 * @see com.yuju.common.core.service.AbstractBaseService#save(java.lang.Object)
	 */
	@Override
	public int saveCheckinStandard(CheckinStandardModel checkinStandardModel,List<PriceDetailModel> priceDetailModels){
		//保存入住标准
		checkinStandardModel.setId(Utils.getUUID());
		checkinStandardModel.setGmtCreate(new Date());
		checkinStandardModel.setGmtModified(new Date());
		this.getDao().saveSelective(checkinStandardModel);
		//全天房入住标准不添加价格明细
		if(Utils.isNotNull(priceDetailModels)){
			//保存价格明细
			for(int i = 0; i < priceDetailModels.size(); i++ ){
				priceDetailModels.get(i).setCheckinStandardId(checkinStandardModel.getId());
				priceDetailModels.get(i).setHotelId(checkinStandardModel.getHotelId());
				priceDetailModels.get(i).setGroupId(checkinStandardModel.getGroupId());
				priceDetailService.save(priceDetailModels.get(i));
			}	
		}
		return 1;
	}
	/**
	 * 
	 * 功能: 更新入住标准<br/>
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2017年11月29日 下午3:08:11<br/>
	 * 
	 * @param checkinStandardModel
	 * @return <br/>
	 * @see com.yuju.common.core.service.AbstractBaseService#updateSelective(java.lang.Object)
	 */
	@Override
	public int updateCheckinStandard(CheckinStandardModel checkinStandardModel,List<PriceDetailModel> priceDetailModels){
		checkinStandardModel.setGmtModified(new Date());
		this.getDao().updateSelective(checkinStandardModel);
		//全天房入住标准不添加价格明细
		if(Utils.isNotNull(priceDetailModels)){
			//保存价格明细
			for(int i = 0; i < priceDetailModels.size(); i++ ){
				priceDetailModels.get(i).setHotelId(checkinStandardModel.getHotelId());
				priceDetailModels.get(i).setGroupId(checkinStandardModel.getGroupId());
				if(Utils.isNotNull(priceDetailModels.get(i).getId())){
					priceDetailService.updateSelective(priceDetailModels.get(i));
				}else {
					priceDetailModels.get(i).setCheckinStandardId(checkinStandardModel.getId());;
					priceDetailService.save(priceDetailModels.get(i));
				}
				
				
				
			}		
		}
		return 1;
	}
	/**
	 * 
	 * 功能: 删除入住标准及相关的价格明细<br/>
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2017年11月29日 下午3:59:44<br/>
	 * 
	 * @param id
	 * @return <br/>
	 * @see com.yuju.common.core.service.AbstractBaseService#del(java.lang.Object)
	 */
	@Override
	public int del(String id){
		this.getDao().del(id);
		priceDetailService.delCheckinStandardDetail(id);
		return 1;
	}
	/**
	 * 
	 * 功能：根据ID获取入住标准的信息<br/>
	 *
	 * @author zhousc
	 * @Date 2017年11月29日 下午4:13:35 <br/>
	 */
	@Override
	public CheckinStandardModel findById(String id){
		CheckinStandard checkinStandard = getDao().getObj(id);
		if (checkinStandard != null && checkinStandard.getCheckinType() != 2) {
			//根据ID查询价格明细
			PriceDetailModel priceDetailModel = new PriceDetailModel();
			priceDetailModel.setCheckinStandardId(id);
			List<PriceDetailModel> priceDetailList = priceDetailService.list(priceDetailModel);
			//转换类型
			CheckinStandardModel checkinStandardModel = convert(checkinStandard);
			checkinStandardModel.setPriceDetailModels(priceDetailList);
			return checkinStandardModel;
		}
		return convert(checkinStandard);
	}

	/**
	 * 
	 * 功能: 新增分店时为分店添加入住标准（散客入住）<br/>
	 * 	
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2017年12月7日 下午2:42:46<br/>
	 * 
	 * @param hotelId:新增分店的ID 
	 * @return <br/>
	 * @see com.yuju.hotel.service.ICheckinStandardService#saveHotelAfter(com.yuju.hotel.model.CheckinStandardModel)
	 */
	@Override
	public int saveHotelAfter(String hotelId,String groupId) {
		CheckinStandardModel checkinStandardModel = new CheckinStandardModel();

		checkinStandardModel.setId(Utils.getUUID());
		checkinStandardModel.setHotelId(hotelId);
		checkinStandardModel.setGroupId(groupId);
		checkinStandardModel.setCheckinType((byte)2);
		checkinStandardModel.setName("散客入住");
		checkinStandardModel.setRemark("该标准为系统默认数据，用户可以根据实际管理需要进行修改");
		checkinStandardModel.setRetentionTime(Time.valueOf("18:00:00"));
		checkinStandardModel.setCheckoutTime(Time.valueOf("12:00:00"));
		checkinStandardModel.setCheckinFullMinute(30);
		checkinStandardModel.setCheckoutHalfMinute(30);
		checkinStandardModel.setCheckoutOverTime(Time.valueOf("18:00:00"));
		checkinStandardModel.setGmtCreate(new Date());
		checkinStandardModel.setGmtModified(new Date());
		
		return this.getDao().saveSelective(checkinStandardModel);
	}

	/**
	 * 
	 * 功能:重名验证 <br/>
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2017年12月14日 上午11:31:13<br/>
	 * 
	 * @param checkinStandardModel
	 * @return <br/>
	 * @see com.yuju.hotel.service.ICheckinStandardService#selectByName(com.yuju.hotel.model.CheckinStandardModel)
	 */
	@Override
	public boolean selectByName(CheckinStandardModel checkinStandardModel) {
		//定义返回值
		boolean result = false;
		//装载数据查询数据库
		CheckinStandardModel model = new CheckinStandardModel();
		model.setName(checkinStandardModel.getName());
		model.setHotelId(checkinStandardModel.getHotelId());
		model.setCheckinType(checkinStandardModel.getCheckinType());
		//修改时验证重名
		if (Utils.isNotNull(checkinStandardModel.getId())) {
			CheckinStandardModel checkinStandardModel1 = convert(this.getDao().getObj(checkinStandardModel.getId()));
			if (checkinStandardModel.getName().equals(checkinStandardModel1.getName())) {
			        result = true;
			    } else {
			   	 	Map<Object, Object> beanMap = getBeanMap(model);
			        List<CheckinStandardModel> list = this.getDao().list(beanMap);
			        if (list.size() == 0) {
			            result = true;
			        }
			    }
		//新增验证重名
		}else {
			Map<Object, Object> beanMap = getBeanMap(model);
			    List<CheckinStandardModel> list = this.getDao().list(beanMap);
			    if (list.size() == 0) {
			        result = true;
			    }
			}
		return result;
	}
}
